/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.preference;

import net.jonbuck.tassoo.ui.nl.Messages;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;

/**
 * 
 * 
 * @since 1.0.0
 */
public class TasksPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private Button btnHideCompletedTasks;
	private IPreferencesService preferenceService;

	private static String[] completedOptions = new String[] {"Delete task(s) immediately", "Delete task(s) after a set period of time", "Do nothing (Manually Delete)"};
	/**
	 * 
	 */
	public TasksPreferencePage() {
		super();
	}

	/**
	 * 
	 */
	public void init(IWorkbench workbench) {
		this.setDescription(Messages.taskspreferencepage_description);
		this.noDefaultAndApplyButton();
		preferenceService = Platform.getPreferencesService();
	}

	/**
	 * 
	 */
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginWidth = 0;
		gl_container.marginRight = 5;
		container.setLayout(gl_container);

		Group grpTasks = new Group(container, SWT.NONE);
		grpTasks.setLayout(new GridLayout(1, false));
		grpTasks.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		grpTasks.setText("Completed Tasks");

		btnHideCompletedTasks = new Button(grpTasks, SWT.CHECK);
		btnHideCompletedTasks.setText("Do not show completed tasks in the Tasks view");
		btnHideCompletedTasks
				.setSelection(preferenceService.getBoolean("net.jonbuck.tassoo", "tasks.filter.complete", false, null));

		Composite wtdCompletedTasks = new Composite(grpTasks, SWT.NONE);
		GridLayout gl_wtdCompletedTasks = new GridLayout(2, false);
		gl_wtdCompletedTasks.marginLeft = 0;
		gl_wtdCompletedTasks.marginRight = 0;
		wtdCompletedTasks.setLayout(gl_wtdCompletedTasks);
		wtdCompletedTasks.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		Label lblWtdCompletedTasks = new Label(wtdCompletedTasks, SWT.NONE);
		lblWtdCompletedTasks.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		lblWtdCompletedTasks.setText("When a task is completed:");
		
		Combo cmbWtdCompletedTasks = new Combo(wtdCompletedTasks, SWT.BORDER);
		cmbWtdCompletedTasks.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		cmbWtdCompletedTasks.setItems(completedOptions);
		cmbWtdCompletedTasks.setText(completedOptions[2]);
		
		return container;
	}

	/**
	 * 
	 */
	public boolean performOk() {
		IEclipsePreferences root = preferenceService.getRootNode();
		root.node(ConfigurationScope.SCOPE).node("net.jonbuck.tassoo")
				.putBoolean("tasks.filter.complete", btnHideCompletedTasks.getSelection());
		try {
			root.flush();
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return super.performOk();
	}

}
